// cloudfunctions/collect-card/index.js
'use strict';
exports.main = async (event, context) => {
  const db = uniCloud.database()
  const { cardId, userId } = event

  // 检查是否已收藏
  const cardRes = await db.collection('cards').doc(cardId).get()
  if (cardRes.data[0].collectUsers.includes(userId)) {
    return { code: 400, message: '您已经收藏过了' }
  }

  // 更新数据库
  await db.collection('cards').doc(cardId).update({
    collectCount: db.command.inc(1),
    collectUsers: db.command.push(userId)
  })
  
  return { code: 200, message: '收藏成功' }
}